package com.maven.front.controller.manageKpointCon;


//    进入章节管理页面前获取当前课程的章节信息
//      对课程进行章节管理操作

import com.maven.front.bean.EduCourseKpoint;
import com.maven.front.bean.FileUploadResult;
import com.maven.front.bean.Result;
import com.maven.front.util.FileUploadService;
import com.maven.front.service.manageCourseKpoint.EduCourseKpointService;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("admin/kpoint")
public class EduCourseKpointController {

    @Autowired
    private EduCourseKpointService eduCourseKpointService;
    @Autowired
    private FileUploadService fileUploadService;

//    根据课程ID查询当前课程所有的章节数据
    @GetMapping("selectByPrimaryKey")
    public Result selectByPrimaryKey(String id){
        log.info("kpoint:"+id);
        return eduCourseKpointService.selectEduCourseKpoint(Integer.valueOf(id));
    }

//    根据章节ID删除章节信息
    @PostMapping("delKpoint")
    public Result delKpoint(String pid){
        return eduCourseKpointService.deleteByPrimaryKey(Integer.valueOf(pid));
    }

//    根据章节ID获取当前要修改的章节信息
    @GetMapping("getById")
    public Result getById(Integer kpointId){
        log.info("id:"+kpointId);
        return eduCourseKpointService.getById(kpointId);
    }

    //    修改章节信息
    @PostMapping("editEduCourseKpoint")
    public Result editEduCourseKpoint(EduCourseKpoint eduCourseKpoint){
        log.info("名称:"+eduCourseKpoint.getName());
        return eduCourseKpointService.updateEduCourseKpoint(eduCourseKpoint);
    }

    //新增当前课程节点信息
    @PostMapping("addZtree")
    public Result addZtree(Integer courseId, String name, Integer ztreeId){
        return eduCourseKpointService.addZtree(courseId, name, ztreeId);
    }


    //    上传课程视频
    @ResponseBody
    @PostMapping("setVideo")
    public Result gameinformationUpdateGameFileMaster2(@RequestParam("file") MultipartFile file) throws Exception {
        Encoder encoder = new Encoder();
        File source = this.multipartFileToFile(file);
        // 获取视频时长
        MultimediaInfo m = encoder.getInfo(source);
        this.delteTempFile(source);
        long ms = m.getDuration();
        long seconds = ms/1000;
        FileUploadResult fileUploadResult = fileUploadService.upload(file);//上传OSS用到的方法
        if (ObjectUtils.isEmpty(fileUploadResult)){
            return new Result(10005, "上传失败！");
        } else {
            Map map = new HashMap();
            map.put("fileUploadResult",fileUploadResult);
            map.put("seconds", seconds);
//            将课程视频信息返回给前台
            return new Result(200, "上传成功！", map);
        }
    }

    //    ---------------------------------------------------获取视频时长-------------------------------------------------------
    //    获取视频时长
    public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本地临时文件
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

}
